﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.WebClient;
using Models.Device;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Reflection.Emit;
using ServiceWeb.APIRequest;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class DeviceParameterInitController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();

        public IActionResult edit(HttpReqModel httpReqModel)
        {
            ViewBag.d_guid = httpReqModel.guid;
            iot_device iot_Device = new DBIotDevice().getDevice(httpReqModel.guid);
            ViewData["commands"] = new DBIotDeviceCommand().getCommandList(iot_Device.d_model_guid);
            return View();
        }



        public RespResultData getDeviceExtInfo(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDevice().getDeviceExtInfo(httpRequest.guid));
        }

        public RespResultData getValueCurrent(HttpReqModel httpRequest)
        {

            return RespResult.Sucess(new DBIotDeviceValueCurrent().getValueCurrentByDevice(httpRequest.guid));
        }

        public RespResultData getDeviceModel(HttpReqModel httpRequest)
        {
            iot_device iot_Device = new DBIotDevice().getDevice(httpRequest.guid);
            return RespResult.Sucess(new DBIotDeviceModel().getModel(iot_Device.d_model_guid));
        }
        public RespResultData getMQList(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBMQLog().getMQListByRows(httpRequest.guid, 10));
        }
        public RespResultData getCommandLog(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceCommandLog().getCommandLogListByRows(httpRequest.guid, 10));
        }

        public RespResultData sendCommand(string dcguid, string dguid)
        {

            iot_device_command command = new DBIotDeviceCommand().getCommand(dcguid);
            //new ServiceCloudDeviceCommand().sendCommandToGateWay(dguid, command.dc_key, "后台-指令发送");
            new ServiceDeviceControl().sendDeviceCommand(dguid, command.dc_key, "后台-指令发送");
            return RespResult.Sucess();
        }
        public RespResultData sendJsonMsg(string msg, string dguid)
        {
            try
            {
                ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(msg);
                ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);
                List<ModelCommandAgreementParameterCommand> commands = UtilsObject.DynamicToEntity<List<ModelCommandAgreementParameterCommand>>(modelCommandAgreement.agreementParameter.command).OrderBy(c => c.order).ToList();
                foreach (ModelCommandAgreementParameterCommand command in commands)
                {
                    new ServiceDeviceControl().sendDeviceCommand(dguid, command.key, "后台-指令发送", msg);
                    //  new ServiceCloudDeviceCommand().sendCommandToGateWayByCommand(dguid, command.key,  "后台-指令发送", msg);
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                return RespResult.Faild(ex.ToString());
            }
            return RespResult.Sucess();
        }
    }
}